﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mirkuz.LocationServices
{
    /// <summary>
    /// Rappresenta una posizione geografica rilevata
    /// </summary>
    public class Location
    {
        public double Latitude = 0;
        public double Longitude = 0;
        public int Altitude = 0;
        public bool AltitudeValid=false;
        public double Heading = 0;
        public bool HeadingValid = false;
        public int Range = 0;        
        public DateTime UTCTimeOfFix = DateTime.Now;
        public bool UTCTimeOfFixValid = false;
        public double Speed = 0;
        public bool SpeedValid = false;

        public Location(double latitude, double longitude)
        {
            Latitude = latitude;
            Longitude = longitude;
        }
        public Location(double latitude, double longitude,DateTime UTCTime)
        {
            Latitude = latitude;
            Longitude = longitude;
            UTCTimeOfFix = UTCTime;
            UTCTimeOfFixValid = true;
        }
        public Location(double latitude, double longitude, int altitude)
        {
            Latitude = latitude;
            Longitude = longitude;
            Altitude = altitude;
            AltitudeValid = true;
        }
        public Location(double latitude, double longitude, int altitude,double speed,double heading)
        {
            Latitude = latitude;
            Longitude = longitude;
            Altitude = altitude;
            AltitudeValid = true;
            SpeedValid = true;
            Speed = speed;
            HeadingValid = true;
            Heading = heading;
        }
        public Location(double latitude, double longitude, int altitude,double speed,double heading, int range, DateTime timeoffix)
        {
            Latitude = latitude;
            Longitude = longitude;
            Altitude = altitude;
            AltitudeValid = true;
            SpeedValid = true;
            Speed = speed;
            HeadingValid = true;
            Heading = heading;
            Range = range;
            UTCTimeOfFix = timeoffix;
            UTCTimeOfFixValid = true;
        }
        /// <summary>
        /// Crea un oggetto location partendo da un oggett GPSPosition di Microsoft.WindowsMobile.Samples.Location
        /// </summary>
        /// <param name="p">GPSPosition di partenza</param>
        /// <returns>Location creata</returns>
        public static Location FromGPSPosition(Microsoft.WindowsMobile.Samples.Location.GpsPosition p)
        {
            Location l=new Location(p.Latitude,p.Longitude);
            if (p.SeaLevelAltitudeValid)
            {
                l.Altitude =(int) p.SeaLevelAltitude;
                l.AltitudeValid = true;
            }
            if (p.HeadingValid)
            {
                l.Heading = p.Heading;
                l.HeadingValid = p.HeadingValid;
            }
            if (p.PositionDilutionOfPrecisionValid)
            {
                l.Range =Location.PDOPToRange( p.PositionDilutionOfPrecision);
            }
            if (p.TimeValid && p.Time != null)
            {
                l.UTCTimeOfFix = p.Time;
                l.UTCTimeOfFixValid = true;
            }
            if (p.SpeedValid)
            {
                l.Speed = p.Speed;
                l.SpeedValid = true;
            }
            return l;
        }

        public static int PDOPToRange(float pdop)
        {
            return(int) (pdop*3.5);
        }
    }
}
